{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一、选股,股池：\n",
    "# (聚宽上我还没实现股池管理)\n",
    "# 1、每日全市场选股入股池:\n",
    "# (1)剔除st,退市、前一交易日停牌的股\n",
    "# (2)选股条件：\n",
    "# 连续三日缩量，当日量小于五日十日十五日均量\n",
    "# (3)若标的已在股池中，选股日期设置为最新日期\n",
    "# (4)每日清除在股池中超过5个交易日的标的\n",
    "# 二、买入：\n",
    "# 1、股池中符合买入条件:\n",
    "# (1)当日量大于前一日量\n",
    "# (2)当日量大于五日十日十五日均量\n",
    "# (3)当日收盘价大于开盘价\n",
    "# (4)当日KDJ金叉\n",
    "# (5)买入前以换手及五日平均换手排序，换手大者优先买入(聚宽上我还没实现)\n",
    "# (6)大盘23个交易日内出现macd死叉不买(刚想到，聚宽上我还没实现)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def FILTERx(COND, N):\n",
    "\n",
    "    k1 = pd.Series(np.where(COND, 1, 0), index=COND.index)\n",
    "    idx = k1[k1 == 1].index.codes[0]\n",
    "    needfilter = pd.Series(idx, index=idx)\n",
    "    afterfilter = needfilter.diff().apply(lambda x: False if x > N else True)\n",
    "    k1.iloc[afterfilter[afterfilter].index] = 2\n",
    "    return k1.apply(lambda x: 1 if x == 2 else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def select1(data):\n",
    "    # 连续三日缩量\n",
    "    ch= data.close[-1] * 1.1\n",
    "    cl= data.close[-1] * 0.9\n",
    "#     ch= data.close * 1.1\n",
    "#     cl = data.close * 0.9\n",
    "    \n",
    "    df=pd.concat([QA.MA(data.close, x) for x in (5,10,20,30,60,90,120,250) ], axis = 1).dropna()[-1:]\n",
    "    df.columns = [u'm5',u'm10',u'm20',u'm30',u'm60',u'm90',u'm120', u'm250']  \n",
    "    df_h = df.apply(lambda x:x.max() <= ch,  axis = 1 )\n",
    "    df_l = df.apply(lambda x:x.min() >= cl,  axis = 1 )\n",
    "    \n",
    "    df['dfh'] = df_h\n",
    "    df['dfl'] = df_l\n",
    "#     out=df.iloc[-1].apply(lambda x: True if x>cl and x < ch else False)\n",
    "\n",
    "    return df\n",
    " \n",
    "#     return pd.DataFrame({\n",
    "# #         't1': t1,\n",
    "# #         't2': t2,\n",
    "# #         't3': t3,\n",
    "# #         'm5': MA_N[0],\n",
    "# #         'm10':  MA_N[1],\n",
    "# #         'm20':  MA_N[2],\n",
    "# #         'm30':  MA_N[3],\n",
    "# #         'm60':  MA_N[4],\n",
    "# #         'm90':  MA_N[5],\n",
    "# #         'm120':  MA_N[6],\n",
    "# #         'm250':  MA_N[7],\n",
    "#         'out':out,\n",
    "# #         'man':MA_N\n",
    "#     })\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#QA.QA_fetch_stock_block_adv().get_block('上证50').code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stock_list = QA.QA_fetch_stock_block_adv().get_block('中小300').code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_list = QA.QA_fetch_stock_list_adv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stocklist_all = stock_list[~stock_list.name.apply(lambda x: 'ALL-ST' in x)].code.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stocklist_all=stocklist_all[1:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stocklist_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#blockname=QA.QA_fetch_stock_block_adv().block_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = QA.QA_fetch_stock_day_adv(stocklist_all,'2010-01-01','2020-11-30')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.selects('000002','2018-06-10').data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# = QA.QA_fetch_stock_day_adv('000001','2019-07-01','2019-09-30')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data.selects('600532','2008-05-28','2008-06-10').data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ind = data.add_func(select1)\n",
    "ind.query('dfh==True and dfl==True')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ind.query('dfh==True and dfl==True')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "drdf=ind.query('dfh==True and dfl==True')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "drdf.to_csv('drnhxg.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = ind.query('res==True').loc[:,['x1','x2','x3','x4']].all(axis=1).groupby(level=1).shift()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1[x1==True]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ind.loc['2008-04-22':'2008-06-19']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
